﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using umbraco;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.member;


// Copyright © 2010 AmplusCorp. All Rights Reserved.

namespace AmplusCorp
{
    public partial class uLogin : System.Web.UI.UserControl
    {
        #region Fields

        //default values
        string _notLoggedInMessage = "<p>Login</p>";
        string _loginText = "Login Name";
        string _loginReqError = "*";
        string _passwordText = "Password";
        string _passwordReqError = "*";
        string _rememberMeText = "Remember Me On This Computer";
        bool _rememberMeSet = false;
        TextAlign _rememberMeTextAlign = TextAlign.Right;
        string _loginButtonText = "Login";
        string _failureText = "Your login attempt was not successful. Please try again.";
        string _loggedInMessage = "Hello";
        bool _loginNameBeforeMessage = false;
        bool _loginNameAfterMessage = true;
        string _logoutText = "Logout";
        int _redirectNodeId = 0;

        #endregion

        #region Properties

        public string notLoggedInMessage
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                {
                    _notLoggedInMessage = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                }
                else
                {
                    _notLoggedInMessage = value;
                }
            }
        }

        public string loginText
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                {
                    _loginText = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                }
                else
                {
                    _loginText = value;
                }
            }
        }

        public string loginReqError
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                {
                    _loginReqError = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                }
                else
                {
                    _loginReqError = value;
                }
            }
        }

        public string passwordText
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                {
                    _passwordText = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                }
                else
                {
                    _passwordText = value;
                }
            }
        }

        public string passwordReqError
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                {
                    _passwordReqError = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                }
                else
                {
                    _passwordReqError = value;
                }
            }
        }

        public string rememberMeText
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                {
                    _rememberMeText = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                }
                else
                {
                    _rememberMeText = value;
                }
            }
        }

        bool rememberMeSet
        {
            set
            {
                _rememberMeSet = value;
            }
        }

        public string rememberMeTextAlign
        {
            set
            {
                if ((value.ToLower() != "right") && (value.ToLower() != "left"))
                {
                    _rememberMeTextAlign = TextAlign.Right;
                }
                else
                {
                    if (value.ToLower() == "right")
                    {
                        _rememberMeTextAlign = TextAlign.Right;
                    }
                    else
                    {
                        _rememberMeTextAlign = TextAlign.Left;
                    }
                }
            }
        }

        public string loginButtonText
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                {
                    _loginButtonText = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                }
                else
                {
                    _loginButtonText = value;
                }
            }
        }

        public string failureText
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                {
                    _failureText = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                }
                else
                {
                    _failureText = value;
                }
            }
        }

        public string loggedInMessage
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                {
                    _loggedInMessage = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                }
                else
                {
                    _loggedInMessage = value;
                }
            }
        }

        public bool loginNameBeforeMessage
        {
            set
            {
                _loginNameBeforeMessage = value;
            }
        }

        public bool loginNameAfterMessage
        {
            set
            {
                _loginNameAfterMessage = value;
            }
        }

        public string logoutText
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                {
                    _logoutText = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                }
                else
                {
                    _logoutText = value;
                }
            }
        }

        public int returnUrl
        {
            set
            {
                _redirectNodeId = value;
            }
        }

        #endregion

        #region Logic

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Member.GetCurrentMember() == null)
                {
                    InitLogin();
                }
                else
                {
                    InitLogout();
                }
            }

        }

        protected void InitLogin()
        {
            InitControls();
            notLoggedInPanel.Visible = true;
            loginFailureLiteral.Visible = false;
            loggedInPanel.Visible = false;
        }

        protected void InitLogout()
        {
            Member currentMember = Member.GetCurrentMember();
            string loginName = currentMember.LoginName;
            string messageText = "";

            if (_loginNameBeforeMessage)
            {
                messageText += loginName;
            }
            if (_loggedInMessage.Length > 0)
            {
                if (messageText.Length > 0)
                {
                    messageText += "&nbsp;";
                }
                messageText += _loggedInMessage;
            }
            if (_loginNameAfterMessage)
            {
                if (messageText.Length > 0)
                {
                    messageText += "&nbsp;";
                }
                messageText += loginName;
            }

            InitControls();
            loggedInMessageLiteral.Text = messageText;
            notLoggedInPanel.Visible = false;
            loggedInPanel.Visible = true;
        }

        protected void InitControls()
        {
            notLoggedInMessageLiteral.Text = _notLoggedInMessage;
            loginNameLabel.Text = _loginText;
            loginNameRequired.ErrorMessage = _loginReqError;
            passwordLabel.Text = _passwordText;
            passwordRequired.ErrorMessage = _passwordReqError;
            rememberMeCheckBox.Text = _rememberMeText;
            rememberMeCheckBox.TextAlign = _rememberMeTextAlign;
            loginButton.Text = _loginButtonText;
            logoutLinkButton.Text = _logoutText;
            loginFailureLiteral.Text = _failureText;
        }

        protected void DoLogin(string loginName, string password, bool rememberMe)
        {

            // valid credientials
            if (System.Web.Security.Membership.ValidateUser(loginName, password))
            {
                // query string - 1st priority
                if (Request.QueryString["ReturnUrl"] != null)
                {
                    System.Web.Security.FormsAuthentication.RedirectFromLoginPage(loginName, rememberMe);
                }

                // redirectNodeId property - 2nd priority
                else if (_redirectNodeId > 0)
                {

                    // valid redirectNodeId - redirect there
                    if (CMSNode.IsNode(_redirectNodeId))
                    {
                        string returnUrl = library.NiceUrl(_redirectNodeId);
                        System.Web.Security.FormsAuthentication.SetAuthCookie(loginName, rememberMe);
                        Response.Redirect(returnUrl, true);
                    }

                    // invalid redirectNodeId - redirect to referrer
                    else
                    {
                        System.Web.Security.FormsAuthentication.SetAuthCookie(loginName, rememberMe);
                        Response.Redirect(Request.UrlReferrer.ToString(), true);
                    }
                }

                // no query string or redirectNodeId property - redirect to referrer
                else
                {
                    System.Web.Security.FormsAuthentication.SetAuthCookie(loginName, rememberMe);
                    Response.Redirect(Request.UrlReferrer.ToString(), true);
                }
            }

            // invalid credientials
            else
            {
                loginFailureLiteral.Visible = true;
            }

        }

        protected void DoLogout()
        {
            System.Web.Security.FormsAuthentication.SignOut();
            Response.Redirect(Request.RawUrl, true);
        }

        protected void loginButton_Click(object sender, EventArgs e)
        {
            DoLogin(loginNameTextBox.Text, passwordTextBox.Text, rememberMeCheckBox.Checked);
        }

        protected void logoutLinkButton_Click(object sender, EventArgs e)
        {
            DoLogout();
        }

        #endregion

    }
}